<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" >
      <el-form-item label="测评名称" >
        <el-input
          v-model="queryParams.evalutionName"
          placeholder="请输入测评名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="targetList" >
      <el-table-column label="测评名称" prop="evalutionName"  :show-overflow-tooltip="true"/>
      <el-table-column label="工号" align="center" prop="userName"  :show-overflow-tooltip="true"/>
      <el-table-column label="述职" align="center" prop="reportFileId" >
        <template slot-scope="scope" v-if="scope.row.reportFileId>0">
          <el-button  size="mini" type="text" @click="viewReport(scope.row)" >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="领导评分" align="center" prop="leaderScore" width="80">
        <template slot-scope="scope" v-if="scope.row.leaderScore">
            <el-button  size="mini" type="text" @click="viewScore(scope.row, 'leader')" >{{scope.row.leaderScore}}</el-button>
          </template>
      </el-table-column>
      <el-table-column label="同事评分" align="center" prop="colleagueScore" width="80">
        <template slot-scope="scope" v-if="scope.row.colleagueScore">
            <el-button  size="mini" type="text" @click="viewScore(scope.row,'colleague')" >{{scope.row.colleagueScore}}</el-button>
          </template>
        </el-table-column>
      <el-table-column label="学生评分" align="center" prop="studentScore" width="80">
        <template slot-scope="scope" v-if="scope.row.studentScore">
            <el-button  size="mini" type="text" @click="viewScore(scope.row, 'student')" >{{scope.row.studentScore}}</el-button>
          </template>
      </el-table-column>
      <el-table-column label="总分" align="center" prop="totalScore" width="80"/>
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleReport(scope.row)"
            v-hasPermi="['evalution:labEvalution:edit']"
          >上传述职</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评试对象对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评测对象" class="form_item">
          {{form.nickName}}&nbsp;/{{form.userName}}
        </el-form-item>
        <el-form-item label="领导评分" class="form_item">
          {{form.leaderScore}}
        </el-form-item>
        <el-form-item label="同事评分" class="form_item">
          {{form.colleagueScore}}
        </el-form-item>
        <el-form-item label="学生评分" class="form_item">
          {{form.studentScore}}
        </el-form-item>
        <el-form-item label="评测关系" prop="relation">
         <el-select v-model="form.relation" placeholder="请选择">
             <el-option
               v-for="item in relationOptions"
               :key="item.value"
               :label="item.label"
               :value="item.value">
             </el-option>
           </el-select>
        </el-form-item>
        <el-form-item label="评测分数" prop="score">
          <el-input v-model="form.score" type="number" placeholder="请输入评测分数" style="width: 200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['evalution:labEvalution:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="上传述职报告" :visible.sync="openUpload" width="460px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="评测对象" >
          {{form.nickName}}:&nbsp;{{form.userName}}
        </el-form-item>
        <el-form-item label="述职报告">
           <UploadCom ref="upload" @submitData="submitData"  description="" :userFileList="fileList" style="width: 300px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="uploadForm" v-hasPermi="['evalution:labEvalution:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <Result ref="resultScore"/>
    <Detail ref="detail" ></Detail>
  </div>
</template>

<script>
  import {getLabEvalution} from "@/api/evalution/labEvalution";
import { listReport, getTarget, updateTarget, scoreTarget } from "@/api/evalution/target";
import UploadCom from "@/views/labdata/commons/upload.vue";
import Result from "@/views/evalution/result/index.vue"
import { treeselect,getTreeLabel } from "@/api/labdata/orgnization";
import Detail from "@/views/evalution/labEvalution/details.vue"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "TargetUser",
  components:{Treeselect,UploadCom,Result,Detail},
  data() {
    return {
      evalutionId:null,
      // 遮罩层
      loading: true,
      // 部门树选项
      deptOptions: [],
      relationOptions:[{
          value: 'leader',
          label: '领导'
        }, {
          value: 'colleague',
          label: '同事'
        }, {
          value: 'student',
          label: '学生'
        }, ],
      // 总条数
      total: 0,
      // 评试对象表格数据
      targetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openUpload:false,
      //附件集合
      fileList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        evalutionId: null,
        userName: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        score: [
          { required: true, message: "评测分数不能为空", trigger: "blur" }
        ],
        relation: [
          { required: true, message: "评测关系不能为空", trigger: "blur" }
        ],
      }
    };
  },
  props:{
    id:Number,
  },
  watch:{
    id:function(val){
      this.evalutionId = val;
      this.getList();
    },
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
       treeselect().then(response => {
         this.deptOptions = response.data;
       });
    },
    /** 查询评试对象列表 */
    getList() {
      this.loading = true;
      this.queryParams.evalutionId = this.evalutionId;
      listReport(this.queryParams).then(response => {
        this.targetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openUpload = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        evalutionId:this.evalutionId,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const participatorId = row.participatorId || this.ids
      getTarget(participatorId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "评测对象评分";
      });
    },
    viewScore(row, recordType){
      this.$refs["resultScore"].getList(row.nickName, row.participatorId, recordType);
    },
    viewReport(row){
      window.location.href=process.env.VUE_APP_PROXY+'/labdata/labFile/image?fileId='+row.reportFileId;
    },
    handleReport(row) {
      this.reset();
      const participatorId = row.participatorId || this.ids
      getTarget(participatorId).then(response => {
        this.form = response.data;
        this.fileList = this.form.images;
        this.openUpload = true;
         this.$forceUpdate();
      });
    },
    handleDetail(row){
      row.instanceId = row.evalutionId;
      this.$refs["detail"].handleUpdate(row);
    },
    uploadForm(){
       this.$refs["upload"].doSubmit();
    },
    submitData(fileList){
      this.form.images = fileList;
      updateTarget(this.form).then(response => {
        this.msgSuccess("述职报告更改成功");
        this.openUpload = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            scoreTarget(this.form).then(response => {
              this.msgSuccess("打分成功");
              this.open = false;
              this.getList();
            });
          }
      });
    },

  }
};
</script>
<style type="text/css" scoped lang="scss">
  .form_item{
      margin-bottom: 5px;
  }
</style>
